package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.config.verification.VdEdit;
import com.hq.ims.data.dao.BusiChangeMapper;
import com.hq.ims.data.entity.BusiChange;
import com.hq.ims.data.vo.BusiChangeMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ObjectUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * @author 蒋柏平
 * @since 20241204
 * @detail 经营信息变更记录 服务实现类
 */
@Service
public class BaseBusiChangeService extends BaseService<BusiChangeMapper, BusiChange> {


    /**
    * 分页
    */
    public List<BusiChangeMapstruct.BusiChangeVo> getByPage(
            RequestPage page,
            BusiChangeMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<BusiChange> lq = this.lambdaQuery();
        //排序
        //查询条件
        lq.eq(BusiChange::getBusiInfoId,searchVo.getBusiInfoId());
        lq.orderByDesc(BusiChange::getChangeTime);
        //执行分页
        lq.page(page);

        List<BusiChange> dataList = page.getRecords();
        List<BusiChangeMapstruct.BusiChangeVo> voList = BusiChangeMapstruct.INSTANCE.toBusiChangeVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
      * 添加
      */
    @Transactional(rollbackFor = Exception.class)
    public BusiChange addData(BusiChangeMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo,VdAdd.class);

        //账值新的对象
        BusiChange entity = new BusiChange();
        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setChangeContent(vo.getChangeContent());
        entity.setChangeBefore(vo.getChangeBefore());
        entity.setChangeAfter(vo.getChangeAfter());
        entity.setChangeWay(vo.getChangeWay());
        entity.setChangeTime(vo.getChangeTime());

         entity.insert();
         return entity;
    }

     /**
     * 编辑
     */
     @Transactional(rollbackFor = Exception.class)
     public BusiChange editData(BusiChangeMapstruct.SaveVo vo) {
         //验证
        SpringBootValidateUtils.validate(vo,VdEdit.class);

        //账值新的对象
        BusiChange entity = this.getById(vo.getBusiChangeId());
        ObjectUtils.isNullThrow(entity, "数据不存在");

        entity.setBusiInfoId(vo.getBusiInfoId());
        entity.setChangeContent(vo.getChangeContent());
        entity.setChangeBefore(vo.getChangeBefore());
        entity.setChangeAfter(vo.getChangeAfter());
        entity.setChangeWay(vo.getChangeWay());
        entity.setChangeTime(vo.getChangeTime());

        entity.updateById();
        return entity;
     }


    /**
    * 删除
    */
    @Transactional(rollbackFor = Exception.class)
    public void delData(String busiChangeId) {
        BusiChange data = this.getById(busiChangeId);
        if(data == null){
            return;
        }
        data.deleteById();
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByBusiId(String busiId) {
        this.lambdaUpdate().eq(BusiChange::getBusiInfoId,busiId).remove();
    }

    /**
     * 获取原始数据详情
     */
    public BusiChangeMapstruct.BusiChangeVo getDetailVo(String busiChangeId) {
        BusiChange entity = this.getById(busiChangeId);
        BusiChangeMapstruct.BusiChangeVo vo = BusiChangeMapstruct.INSTANCE.toBusiChangeVo(entity);
        return vo;
    }

}